Chomsky normal form

In formal language theory, a context-free grammar is said to be in Chomsky normal form if all of its production rules are of the form:

A \rightarrow BC or
A \rightarrow \alpha or
S \rightarrow \varepsilon

where A, B and C are nonterminal symbols, α is a terminal symbol (a symbol that represents a constant value), S is the start symbol, and ε is the empty string. Also, neither B nor C may be the start symbol.

Every grammar in Chomsky normal form is context-free, and conversely, every context-free grammar can be transformed into an equivalent one which is in Chomsky normal form. Several algorithms for performing such a transformation are known. Transformations are described in most textbooks on automata theory, such as (Hopcroft and Ullman, 1979). As pointed out by Lange and Leiß, the drawback of these transformations is that they can lead to an undesirable bloat in grammar size. The size of a grammar is the sum of the sizes of its production rules, where the size of a rule is one plus the length of its right-hand side. Using |G| to denote the size of the original grammar G, the size blow-up in the worst case may range from |G|^2 to 2^{2 |G|}, depending on the transformation algorithm used (Lange and Leiß, 2009).

Contents

Alternative definition

Another way to define Chomsky normal form (e.g., Hopcroft and Ullman 1979, and Hopcroft et al. 2006) is:

A formal grammar is in Chomsky reduced form if all of its production rules are of the form:

A \rightarrow\, BC or
A \rightarrow\, \alpha

where A, B and C are nonterminal symbols, and α is a terminal symbol. When using this definition, B or C may be the start symbol. Only those context-free grammars which do not generate the empty string, can be transformed into Chomsky reduced form.

Converting a grammar to Chomsky Normal Form

  1. Introduce S_0
    Introduce a new start variable, S_0 and a new rule S_0 \rightarrow S where S is the previous start variable.
  2. Eliminate all \epsilon rules
    \epsilon rules are rules of the form A \rightarrow \epsilon where A \not= S_0 and A \in V where V is the CFG's variable alphabet.
    Remove every rule with \epsilon on its right hand side (RHS). For each rule with A in its RHS, add a set of new rules consisting of the different possible combinations of A replaced or not replaced with \epsilon. If a rule has A as a singleton on its RHS, add a new rule R = A \rightarrow \epsilon unless R has already been removed through this process. For example, examine the following grammar G:
    S \rightarrow AbA | B
    B \rightarrow b | c
    A \rightarrow \epsilon
    G has one \epsilon rule. When the A \rightarrow \epsilon is removed, we get the following:
    S \rightarrow AbA | Ab | bA | b | B
    B \rightarrow b | c
    Notice that we have to account for all possibilities of A \rightarrow \epsilon and so we actually end up adding 3 rules.
  3. Eliminate all unit rules
    A \rightarrow B \ni A,B \in V
    After all the \epsilon rules have been removed, you can begin removing unit rules, or rules whose RHS contains one variable and no terminals (which is inconsistent with CNF).
    To remove A \rightarrow B
    \forall B \rightarrow U add rule A \rightarrow U unless this is a unit rule which has already been removed.
  4. Clean up remaining rules that are not in Chomsky normal form.
    Replace Failed to parse (unknown function\dotso): A \rightarrow u_1 u_2 \dotso u_k, k \ge 3, u_1 \in V \cup \Sigma
with Failed to parse (unknown function\dotsc): A \rightarrow u_1 A_1 , A_1 \rightarrow u_2 A_2 , \dotsc , A_{k-2} \rightarrow u_{k-1} u_k
where A_i are new variables.
  1. If u_i \in \Sigma, replace u_i in above rules with some new variable v_i and add rule v_i \rightarrow u_i.

See also

References